package main

import (
	"fmt"
	"io/ioutil"
)

func demo_if() {
	const filename = "chapter02-basic/branch/abc.txt"
	//contents, err := ioutil.ReadFile(filename)
	//if err!=nil{
	//	fmt.Println(err)
	//}else {
	//	fmt.Printf("%s \n", contents)
	//}

	//if的条件里可以赋值
	//if条件里的赋值，其作用域就在if这个语句里
	if contents, err := ioutil.ReadFile(filename); err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%s \n", contents)
	}

}

func grade(score int) string {
	g := ""
	// switch可以不接表达式
	// go里，默认自己在每个case后加了break
	// 如果不想要，需要自己加上fallthrough
	switch {
	case score < 0 || score > 100:
		panic(fmt.Sprintf("Wrong Score: %d", score))
	case score < 60:
		g = "F"
	case score < 80:
		g = "C"
	case score < 90:
		g = "B"
	case score <= 100:
		g = "A"
	}
	return g
}

func demo_switch() {
	fmt.Println(
		grade(0),
		grade(59),
		grade(60),
		grade(82),
		grade(99),
		grade(100),
		//grade(101),
	)
}

func main() {
	demo_if()
	demo_switch()
}
